package orbito.physics.motion;

import java.util.Collection;
import java.util.LinkedList;


public class DefaultForceManager implements ForceManager {
	
	Collection<Actor> actors;
	Collection<AbstractForce> forces;
	
	public DefaultForceManager() {
		actors = new LinkedList<Actor>();
		forces = new LinkedList<AbstractForce>();
	}
	
	public void addForce(AbstractForce force) {
		// Add to forces.
		forces.add(force);
		
		// Add to flat-list actors.
		actors.addAll(force.getActors());
	}
	
	public void removeForce(AbstractForce force) {
		forces.remove(force);
	}
	
	public void removeActor(Actor actor) {
		actors.remove(actor);
	}
	
	public Collection<Actor> getActors() {
		return actors;
	}
	
	public void calculateForces(double t) {
		// 1. Clear all previously calculated forces.
		for (Actor a : actors) {
			a.clearAccumulatedForces();
		}
		
		// 2. Apply all forces to their actors.
		for (AbstractForce f : forces) {
			f.applyForces();
		}
	}
}
